package com.ding.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author ding
 * @date 2021/7/17 21:04
 */


public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//new Thread(new Runnable())

        MyThread thread = new MyThread();
        FutureTask<String> futureTask = new FutureTask<>(thread);//适配

        new Thread(futureTask).start();
        new Thread(futureTask).start();  //有缓存
        String s = futureTask.get();//可能会阻塞，（异步）
        System.out.println("返回结果是："+s);

    }
}


class MyThread implements Callable<String> {
    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    @Override
    public String call() throws Exception {
        System.out.println("call（）");
        return "45678";
    }
}